package com.nannybear.Model;

import java.util.HashMap;

/**
 * \brief Manage the events (creation and suppression)
 * \author Audrey LOISEAU
 * \date 11-05-2015
 */

/********************************* COPYRIGHT *********************************
 * Copyright (c) 2015, Audrey LOISEAU and Benjamin LANCINOT.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *************************************************************************************/

public class EventsCalendar {
    /********** DECLARATIONS **********/
    private HashMap<String, Event[]> mapEvent;


    /************ METHODS ************/
    /**
     * \brief Constructor
     */
    public EventsCalendar(){
        mapEvent = new HashMap<>();
    }

    /**
     * \brief Add an event in the hash map
     * \param date  The date of the event
     * \param icon  The icon of the event
     * \param event  The title of the event
     * \param time  The time of the event
     */
    public void addEvent(String date, String icon, String event, String time){
        Event newEvent =new Event();
        newEvent.icon = icon;
        newEvent.event = event;
        newEvent.time = time;
        // If the date exist, add the event in the hash map
        if (mapEvent.get(date) != null){
            Event[] temp = new Event[mapEvent.get(date).length +1];
            for(int i=0;i<temp.length-1;i++){
               temp[i]= mapEvent.get(date)[i];
            }
            temp[temp.length-1] = newEvent;
            mapEvent.put(date, temp);
        }
        // Create a new event at this date
        else{
            Event[] temp = new Event[1];
            temp[0] = newEvent;
            mapEvent.put(date, temp);
        }
    }

    /**
     * \brief Delete an event in the hash map
     * \param date  The date of the event to delete
     * \param posEvent  The position of the event in the list view
     */
    public void removeEvents(String date, int posEvent){
        // If there is more than one event at this date
        if(mapEvent.get(date).length > 1) {
            Event[] temp = new Event[mapEvent.get(date).length - 1];
            for (int i = 0; i < posEvent; i++) {
                temp[i] = mapEvent.get(date)[i];
            }
            for (int i = posEvent + 1; i < mapEvent.get(date).length; i++) {
                temp[i - 1] = mapEvent.get(date)[i];
            }
            mapEvent.put(date, temp);
        }
        // If there is just one event in the list at this date
        else{
            mapEvent.remove(date);
        }
    }

    /*********** ACCESSORS ***********/
    /**
     * \brief Get method for the event at the good date
     */
    public Event[] getEvent(String date){
       return mapEvent.get(date);
    }

    /**
     * \brief Get method for the events map
     */
    public HashMap<String, Event[]> getMapEvent() {
        return mapEvent;
    }
}
